#!/bin/zsh

LDAP_LOG_LEVEL=${LDAP_LOG_LEVEL:-"64"}
LDAP_DOMAIN=${LDAP_DOMAIN-"domain"}
LDAP_SUBDOMAIN=${LDAP_SUBDOMAIN:-"openldap"}
LDAP_USER=${LDAP_USER:-"ldap"}
LDAP_GROUP=${LDAP_GROUP:-"ldap"}
LDAP_CONFIG_DIR=${LDAP_CONFIG_DIR:-"/etc/openldap/slapd.d"}
LDAP_URL=${LDAP_URL:-"ldap:///"}
LDAP_ROOT_DN=${LDAP_ROOT_DN:-"cn=root,dc=${LDAP_SUBDOMAIN},dc=${LDAP_DOMAIN}"}
LDAP_ROOT_PASSWORD_INPUT=${LDAP_ROOT_PASSWORD_INPUT:="123321"}
# LDAP_ROOT_PASSWORD=`slappasswd -s ${LDAP_ROOT_PASSWORD_INPUT}`
LDAP_ROOT_PASSWORD=${LDAP_ROOT_PASSWORD_INPUT}
# unset LDAP_ROOT_PASSWORD_INPUT

OPENLDAP_INSTALLED=0
function install_openldap  {
	install -m 755 -o ldap -g ldap -d ${LDAP_CONFIG_DIR}
	# Open up /etc/conf.d/slapd in your favorite editor
	# Comment out cfgfile="/etc/openldap/slapd.conf"
	sed -i 's,^cfgfile=,#cfgfile=,g' /etc/conf.d/slapd 
	# Uncomment cfgdir="/etc/openldap/slapd.d"
	sed -i 's,^#cfgdir=.*$,cfgdir="/etc/openldap/slapd.d",g' /etc/conf.d/slapd 

	[ -f /etc/openldap/slapd.conf ] && rm /etc/openldap/slapd.conf

	# Open up /etc/openldap/slapd.ldif in your favorite editor. 
	# Search for the file names ending with .la and change the extension to .so
	sed 's/([a-zA-Z0-9]*).la/\1.so/g' /etc/openldap/slapd.ldif > /tmp/slapd.ldif
	diff /etc/openldap/slapd.ldif /tmp/slapd.ldif > /dev/null 

	# Edit slapd.ldif again.
	echo "LDAP_SUBDOMAIN = ${LDAP_SUBDOMAIN}"
	echo "LDAP_DOMAIN = ${LDAP_DOMAIN}"
	# echo "Before Replace"
	# sed -n '/^olcSuffix:/p' /etc/openldap/slapd.ldif 
	# sed -n '/^olcRootDN: cn=\(.*\),dc=\(.*\),dc=\(.*\)/p' /etc/openldap/slapd.ldif 
	# Find the olcSuffix: keyword.
	# Change the value to match your domain
	sed -i "s/^olcSuffix: dc=\(.*\),dc=\(.*\)$/olcSuffix: dc="${LDAP_SUBDOMAIN}",dc="${LDAP_DOMAIN}"/g" /etc/openldap/slapd.ldif 
	# Find olcRootDN:
	# Change the value to match your domain
	sed -i "s/^olcRootDN: cn=\(.*\),dc=\(.*\),dc=\(.*\)$/olcRootDN: "${LDAP_ROOT_DN}"/g" /etc/openldap/slapd.ldif 
	sed -i "s/^olcRootPW: secret/olcRootPW: "${LDAP_ROOT_PASSWORD}"/g" /etc/openldap/slapd.ldif 
	# echo "After Replace"
	# sed -n '/^olcSuffix:/p' /etc/openldap/slapd.ldif 
	# sed -n '/^olcRootDN: cn=\(.*\),dc=\(.*\),dc=\(.*\)/p' /etc/openldap/slapd.ldif 

	# Edit slapd.ldif again.
	# Find the line include: file:///etc/openldap/schema/core.ldif
	# Append similar lines referencing cosine.ldif, inetorgperson.ldif, and nis.ldif.


	# Importing ldif
	rm -f ${LDAP_CONFIG_DIR}/cn=config.ldif
	rm -rf ${LDAP_CONFIG_DIR}/cn=config/
	slapadd -n 0 -F ${LDAP_CONFIG_DIR} -l /etc/openldap/slapd.ldif

	chown -R ldap:ldap ${LDAP_CONFIG_DIR}/*

	install -m 755 -o ldap -g ldap -d /var/lib/openldap/run
	OPENLDAP_INSTALLED=1
	echo "Installed openldap"
	echo "LDAP_ROOT_PASSWORD = ${LDAP_ROOT_PASSWORD}"
}

function run_openldap() {
	if [ ${OPENLDAP_INSTALLED} -eq 0 ]; then
		install_openldap
	fi
	run_phpldapadmin
	if [ $1 = "-d" ]; then
		exec /usr/sbin/slapd -d ${LDAP_LOG_LEVEL} -u ${LDAP_USER} -g ${LDAP_GROUP} -F ${LDAP_CONFIG_DIR} -h ${LDAP_URL}
	else
		/usr/sbin/slapd -d ${LDAP_LOG_LEVEL} -u ${LDAP_USER} -g ${LDAP_GROUP} -F ${LDAP_CONFIG_DIR} -h ${LDAP_URL}
	fi
}

function run_phpldapadmin () {
	run_nginx
	install_phpldapadmin
	echo "Running phpldapadmin"
	php-fpm83 
}

function install_phpldapadmin {
	apk add --no-cache phpldapadmin php-fpm php83-ldap php83-session php83-gettext php83-xml
	# sed -i 's/;extension=ldap/extension=ldap/g' /etc/php83/php.ini
	[ -f /etc/nginx/http.d/default.conf ] && rm /etc/nginx/http.d/default.conf
	[ -f /srv/phpldapadmin.conf ] && cp /srv/phpldapadmin.conf /etc/nginx/http.d/phpldapadmin.conf
	if [ -f /srv/config.php ]; then
		cp /srv/phpldapadmin_config.php /etc/phpldapadmin/config.php
	else
		cp /etc/phpldapadmin/config.php.example /etc/phpldapadmin/config.php
	fi
	echo "Installed phpldapadmin"
}

function run_nginx () {
	install_nginx
	echo "Running nginx"
	/usr/sbin/nginx
}

function install_nginx {
	install_zsh
	apk add --no-cache nginx
	echo "Installed nginx"
}

function install_zsh {
	install_tmux
	apk add --no-cache zsh
	echo > ${HOME}/.zshrc <<EOF
#!/bin/zsh
export PROMPT='%n@%m %1~ %#  '
EOF
	source ~/.zshrc
	echo "Installed zsh"
}

function install_tmux {
	apk add --no-cache tmux
}

install_zsh

